package com.uhylab.uhuvinetest.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.uhylab.uhuvinetest.JNIJson;
import com.uhylab.uhuvinetest.R;
import com.uhylab.uhuvinetest.activity.UHCYDoctorActivity;
import com.uhylab.uhuvinetest.activity.UHHistoryActivity;
import com.uhylab.uhuvinetest.activity.UHU2DetailActivity;
import com.uhylab.uhuvinetest.base.UHBase;
import com.uhylab.uhuvinetest.bean.History_Content;
import com.uhylab.uhuvinetest.dao.CloudHistory;
import com.uhylab.uhuvinetest.utils.sharedpreutils.SharedPreUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import okhttp3.Call;

/**
 * Created by xyb on 15/12/14 at UHylab
 *
 * @version 1.0
 */
public class UHHistory_List_Adapter extends BaseAdapter {

    private final String userToken;
    private final Resources resources;
    private List<CloudHistory> data;
    private LayoutInflater mInflater;
    private Context context;
    private JNIJson jniJson;
    private float aFloat;
    private float aFloat1;
    private float aFloat2;
    private float aFloat3;
    private float aFloat4;
    private float aFloat5;
    private float aFloat6;
    private float aFloat7;
    private float aFloat8;
    private float aFloat9;
    private float aFloat10;
    private ArrayList<History_Content> arrayListContent;

    @SuppressLint("HandlerLeak")
    public UHHistory_List_Adapter(Context context, List<CloudHistory> data) {
        this.data = data;
        this.context = context;
        mInflater = LayoutInflater.from(context);
        userToken = SharedPreUtils.getString(context, "user_token", null);
        resources = context.getResources();
    }

    @Override
    public int getCount() {
        return data != null && data.size() > 0 ? data.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return data != null && data.size() > 0 ? data.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return data != null && data.size() > 0 ? position : 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.fragment_history_list_item, null);
            holder.history_date = (TextView) convertView.findViewById(R.id.history_date);
            holder.history_time = (TextView) convertView.findViewById(R.id.history_time);
            holder.rippleView = (RelativeLayout) convertView.findViewById(R.id.rippleView);
            holder.history_type_LinearL = (LinearLayout) convertView.findViewById(R.id.history_type_LinearL);
            holder.u1oru2 = (ImageView) convertView.findViewById(R.id.u1oru2);
            holder.ask_img = (ImageView) convertView.findViewById(R.id.ask_img);
            holder.ask_doctor = (TextView) convertView.findViewById(R.id.ask_doctor);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.history_type_LinearL.removeAllViews();
        if (data != null && data.size() > 0) {
            String date = data.get(position).getCreatetime();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
                initListener(position, holder.rippleView, holder.history_date, holder.history_time);
                if (!(date == null || date.equals(""))) {
                    Date parse = sdf.parse(date);
                    DateFormat timeData = new SimpleDateFormat("yyyy年MM月dd日");
                    final DateFormat time = new SimpleDateFormat("HH:mm");
                    String format = timeData.format(parse);
                    String format1 = time.format(parse);
                    holder.history_date.setText(format);
                    holder.history_time.setText(format1);
                    final Long hisID = data.get(position).getId();
//                    Log.e("UHHistory_List_Adapter", "hisID:" + hisID);
                    OkHttpUtils.get()
                            .url(UHBase.chunYuDoctorHisApi)
                            .addParams("recordid", Long.toString(hisID))
                            .build()
                            .execute(new StringCallback() {
                                @Override
                                public void onError(Call call, Exception e, int id) {
                                    Log.e("UHHistory_List_Adapter", e.toString());

                                }

                                @Override
                                public void onResponse(String response, int id) {
//                                    Log.e("UHHistory_List_Adapter", response);
                                    try {
                                        boolean isCreate = false;
                                        int status;
                                        String problem_id = null;
                                        JSONObject jsonObject = new JSONObject(response);
                                        if (jsonObject.get("code").equals("0000")) {
                                            isCreate = true;
                                            JSONObject problem = (JSONObject) jsonObject.get("data");
                                            status = (int) problem.get("status");
                                            if (status == 1) {
                                                holder.ask_doctor.setText("已联系");
                                                holder.ask_img.setImageResource(R.mipmap.ask_on);
                                            } else if (status == 2) {
                                                holder.ask_doctor.setText("查看回复");
                                                holder.ask_img.setImageResource(R.mipmap.ask_on);

                                            } else if (status == 3) {
                                                holder.ask_img.setImageResource(R.mipmap.ask_off);
                                                holder.ask_doctor.setText("已关闭");

                                            }
                                            problem_id = (String) problem.get("problem_id");
                                        } else if (jsonObject.get("code").equals("0001")) {
                                            isCreate = false;
                                            holder.ask_img.setImageResource(R.mipmap.ask_take);
                                            holder.ask_doctor.setText("联系医生");

                                        }

                                        final boolean finalIsCreate = isCreate;
                                        final String finalProblem_id = problem_id;
                                        holder.ask_doctor.setOnClickListener(new View.OnClickListener() {
                                            @Override
                                            public void onClick(View v) {
                                                fisss(position, holder.history_date, holder.history_time, false);
                                                Intent intent = new Intent(context, UHCYDoctorActivity.class);
                                                intent.putExtra("id", hisID);
                                                intent.putExtra("token", userToken);
                                                intent.putExtra("isCreate", finalIsCreate);
                                                intent.putExtra("problem_id", finalProblem_id);
                                                intent.putParcelableArrayListExtra("content", arrayListContent);
                                                context.startActivity(intent);
                                            }
                                        });
                                    } catch (JSONException e) {
                                        e.printStackTrace();
                                    } catch (ClassCastException e) {
                                        e.printStackTrace();
                                    }


                                }
                            });


                }

            } catch (ParseException e) {
                e.printStackTrace();
                holder.history_date.setText("未知");
                holder.history_time.setText("未知");
            }

            for (int i = 0; i < 11; i++) {
                ImageView imageView = new ImageView(context);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, 52);
                if (i == 0) {
                    params.leftMargin = 24;
                } else {
                    params.leftMargin = 10;
                }
                if (i == 11 - 1) {
                    params.rightMargin = 24;
                }
                params.topMargin = 42;
                params.bottomMargin = 64;
                params.weight = 1;
                imageView.setLayoutParams(params);
                switch (i) {
                    case 0:
                        initImg(data.get(position).getLeukocytes_status(), imageView);
                        holder.history_type_LinearL.addView(imageView);
                        break;
                    case 1:
                        initImg(data.get(position).getNitrite_status(), imageView);
                        holder.history_type_LinearL.addView(imageView);
                        break;
                    case 2:
                        initImg(data.get(position).getUrobilinogen_status(), imageView);
                        holder.history_type_LinearL.addView(imageView);
                        break;
                    case 3:
                        initImg(data.get(position).getProtein_status(), imageView);
                        holder.history_type_LinearL.addView(imageView);
                        break;
                    case 4:
                        if (!data.get(position).getPh_status().equals("-1")) {
                            initImg(data.get(position).getPh_status(), imageView);
                            holder.history_type_LinearL.addView(imageView);
                            holder.u1oru2.setImageResource(R.drawable.lillter_u1);
                        } else {
                            holder.u1oru2.setImageResource(R.drawable.lillter_u2);
                        }
                        //initImg(data.get(position).getPh_status(), imageView);
                        break;
                    case 5:
                        if (!data.get(position).getBlood_status().equals("-1")) {
                            initImg(data.get(position).getBlood_status(), imageView);
                            holder.history_type_LinearL.addView(imageView);
                        }
                        //initImg(data.get(position).getBlood_status(), imageView);
                        break;
                    case 6:
                        if (!data.get(position).getGravity_status().equals("-1")) {
                            initImg(data.get(position).getGravity_status(), imageView);
                            holder.history_type_LinearL.addView(imageView);
                        }
                        // initImg(data.get(position).getGravity_status(), imageView);
                        break;
                    case 7:
                        if (!data.get(position).getKetones_status().equals("null")) {
                            initImg(data.get(position).getKetones_status(), imageView);
                            holder.history_type_LinearL.addView(imageView);
                        }
                        //initImg(data.get(position).getKetones_status(), imageView);
                        break;
                    case 8:
                        if (!data.get(position).getBilirubin_status().equals("-1")) {
                            initImg(data.get(position).getBilirubin_status(), imageView);
                            holder.history_type_LinearL.addView(imageView);
                        }
                        //initImg(data.get(position).getBilirubin_status(), imageView);
                        break;
                    case 9:
                        if (!data.get(position).getGlucose_status().equals("-1")) {
                            initImg(data.get(position).getGlucose_status(), imageView);
                            holder.history_type_LinearL.addView(imageView);
                        }
                        // initImg(data.get(position).getGlucose_status(), imageView);
                        break;
                    case 10:
                        if (!data.get(position).getAscorbicacid_status().equals("-1")) {
                            initImg(data.get(position).getAscorbicacid_status(), imageView);
                            holder.history_type_LinearL.addView(imageView);
                        }
                        //initImg(data.get(position).getAscorbicacid_status(), imageView);
                        break;
                }
                //holder.history_type_LinearL.addView(imageView);
            }
        }
        return convertView;
    }

    private void initListener(final int position, RelativeLayout view, final TextView day, final TextView time) {
        jniJson = new JNIJson(context);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (data.get(position).getPh_status().equals("-1")) {
                    JNIJson jniJson = new JNIJson(context);
                    final String[] contents = {
                            jniJson.getLeukocytes().get(get_near(jniJson.getS(), Float.parseFloat(data.get(position).getLeukocytes()))).getContentValue()
                                    + "" + jniJson.getLeukocytes().get(get_near(jniJson.getS(), Float.parseFloat(data.get(position).getLeukocytes()))).getUnit(),
                            jniJson.getNitrite().get(get_near(jniJson.getNis(), Float.parseFloat(data.get(position).getNitrite()))).getContentValue()
                                    + "" + jniJson.getNitrite().get(get_near(jniJson.getNis(), Float.parseFloat(data.get(position).getNitrite()))).getUnit(),
                            jniJson.getUrobilinogen().get(get_near(jniJson.getUrs(), Float.parseFloat(data.get(position).getUrobilinogen()))).getContentValue() + ""
                                    + jniJson.getUrobilinogen().get(get_near(jniJson.getUrs(), Float.parseFloat(data.get(position).getUrobilinogen()))).getUnit(),
                            jniJson.getProtein().get(get_near(jniJson.getPrs(), Float.parseFloat(data.get(position).getProtein()))).getContentValue() + "" +
                                    jniJson.getProtein().get(get_near(jniJson.getPrs(), Float.parseFloat(data.get(position).getProtein()))).getUnit(),
                    };
                    String status[] = {
                            data.get(position).getLeukocytes_status(),
                            data.get(position).getNitrite_status(),
                            data.get(position).getUrobilinogen_status(),
                            data.get(position).getProtein_status()
                    };
                    Intent intent = new Intent(context, UHU2DetailActivity.class);
                    intent.putExtra("contents", contents);
                    intent.putExtra("status", status);
                    intent.putExtra("time", day.getText().toString() + "  " + time.getText().toString());
                    context.startActivity(intent);
                    for (int i = 0; i < 4; i++) {
                        Log.e("TAG", contents[i] + "   " + status[i]);
                    }
                } else {
                    fisss(position, day, time, true);
                }
            }
        });
    }

    public void fisss(int position, final TextView day, final TextView time, boolean isIntent) {
        arrayListContent = new ArrayList<>();
        History_Content leukocytes = new History_Content();
        History_Content nitrite = new History_Content();
        History_Content urobilinogen = new History_Content();
        History_Content protein = new History_Content();
        History_Content ph = new History_Content();
        History_Content blood = new History_Content();
        History_Content gravity = new History_Content();
        History_Content ketones = new History_Content();
        History_Content bilirubin = new History_Content();
        History_Content glucose = new History_Content();
        History_Content ascorbicacid = new History_Content();
        History_Content[] history_contents = {
                leukocytes, nitrite, urobilinogen, protein, ph, blood, gravity, ketones, bilirubin, glucose, ascorbicacid
        };
        String[] strings = {
                "白细胞", "亚硝酸盐", "尿胆原", "蛋白质", "酸碱度", "潜血", "比重", "酮体", "胆红素", "葡萄糖", "抗坏血酸"
        };
        try {
            CloudHistory dataEntity = data.get(position);
//            aFloat = (Float.parseFloat(dataEntity.getLeukocytes()) > 180) && (Float.parseFloat(dataEntity.getLeukocytes()) < 260) ? Float.parseFloat(dataEntity.getLeukocytes()) + 180 : (Float.parseFloat(dataEntity.getLeukocytes()) + 360);
            if ((Float.parseFloat(dataEntity.getLeukocytes()) > 180) && (Float.parseFloat(dataEntity.getLeukocytes()) < 240)) {
                aFloat = (Float.parseFloat(dataEntity.getLeukocytes()) + 180);
            } else if (Float.parseFloat(dataEntity.getLeukocytes()) < 180) {
                aFloat = (Float.parseFloat(dataEntity.getLeukocytes()) + 360);
            } else if (Float.parseFloat(dataEntity.getLeukocytes()) >= 240) {
                aFloat = (Float.parseFloat(dataEntity.getLeukocytes()));
            }
//            aFloat1 = (Float.parseFloat(dataEntity.getNitrite()) > 180) && (Float.parseFloat(dataEntity.getNitrite()) < 260) ? Float.parseFloat(dataEntity.getNitrite()) + 180 : (Float.parseFloat(dataEntity.getNitrite()) + 360);
            if ((Float.parseFloat(dataEntity.getNitrite()) > 180) && (Float.parseFloat(dataEntity.getNitrite()) < 240)) {
                aFloat1 = (Float.parseFloat(dataEntity.getNitrite()) + 180);
            } else if (Float.parseFloat(dataEntity.getNitrite()) < 180) {
                aFloat1 = (Float.parseFloat(dataEntity.getNitrite()) + 360);
            } else if (Float.parseFloat(dataEntity.getNitrite()) >= 240) {
                aFloat1 = (Float.parseFloat(dataEntity.getNitrite()));
            }


            aFloat2 = Float.parseFloat(dataEntity.getUrobilinogen()) > 180 ? Float.parseFloat(dataEntity.getUrobilinogen()) : (Float.parseFloat(dataEntity.getUrobilinogen()) + 360);

//            aFloat3 = Float.parseFloat(dataEntity.getProtein()) > 180 ? Float.parseFloat(dataEntity.getProtein()) : (Float.parseFloat(dataEntity.getProtein()) + 360);
            aFloat3 = Float.parseFloat(dataEntity.getProtein());
            aFloat4 = Float.parseFloat(dataEntity.getPh()) > 180 ? Float.parseFloat(dataEntity.getPh()) : (Float.parseFloat(dataEntity.getPh()) + 360);
            aFloat5 = Float.parseFloat(dataEntity.getBlood()) > 180 ? Float.parseFloat(dataEntity.getBlood()) : (Float.parseFloat(dataEntity.getBlood()) + 360);
            aFloat6 = Float.parseFloat(dataEntity.getGravity()) > 180 ? Float.parseFloat(dataEntity.getGravity()) : (Float.parseFloat(dataEntity.getGravity()) + 360);
//            aFloat7 = (Float.parseFloat(dataEntity.getKetones()) > 180) && (Float.parseFloat(dataEntity.getKetones()) < 260) ? Float.parseFloat(dataEntity.getKetones()) + 180 : (Float.parseFloat(dataEntity.getKetones()) + 360);

            if ((Float.parseFloat(dataEntity.getKetones()) > 180) && (Float.parseFloat(dataEntity.getKetones()) < 240)) {
                aFloat7 = (Float.parseFloat(dataEntity.getKetones()) + 180);
            } else if (Float.parseFloat(dataEntity.getKetones()) < 180) {
                aFloat7 = (Float.parseFloat(dataEntity.getKetones()) + 360);
            } else if (Float.parseFloat(dataEntity.getKetones()) >= 240) {
                aFloat7 = (Float.parseFloat(dataEntity.getKetones()));
            }

//            aFloat8 = Float.parseFloat(dataEntity.getBilirubin()) > 180 ? Float.parseFloat(dataEntity.getBilirubin()) : (Float.parseFloat(dataEntity.getBilirubin()) + 360);
//            aFloat8 = Float.parseFloat(dataEntity.getBilirubin());
//            if ((aFloat8 < 20)) {
//            aFloat8 = aFloat8 + 20;


            if ((Float.parseFloat(dataEntity.getBilirubin()) > 180) && (Float.parseFloat(dataEntity.getBilirubin()) < 240)) {
                aFloat8 = (Float.parseFloat(dataEntity.getBilirubin()) + 180);
            } else if (Float.parseFloat(dataEntity.getBilirubin()) < 180) {
                aFloat8 = (Float.parseFloat(dataEntity.getBilirubin()) + 360);
            } else if (Float.parseFloat(dataEntity.getBilirubin()) >= 240) {
                aFloat8 = (Float.parseFloat(dataEntity.getBilirubin()));
            }
//            }
            aFloat9 = Float.parseFloat(dataEntity.getGlucose());
            aFloat10 = Float.parseFloat(dataEntity.getAscorbicacid()) > 180 ? Float.parseFloat(dataEntity.getAscorbicacid()) : (Float.parseFloat(dataEntity.getAscorbicacid()) + 360);


            String[] floats = {
                    jniJson.getLeukocytes().get(getNear(jniJson.getS(), aFloat)).getContentValue() + ""
                            + jniJson.getLeukocytes().get(getNear(jniJson.getS(), aFloat)).getUnit(),
                    jniJson.getNitrite().get(getNear(jniJson.getNis(), aFloat1)).getContentValue() + ""
                            + jniJson.getNitrite().get(getNear(jniJson.getNis(), aFloat1)).getUnit(),
                    jniJson.getUrobilinogen().get(getNear(jniJson.getUrs(), aFloat2)).getContentValue() + ""
                            + jniJson.getUrobilinogen().get(getNear(jniJson.getUrs(), aFloat2)).getUnit(),
                    jniJson.getProtein().get(getNear(jniJson.getPrs(), aFloat3)).getContentValue() + ""
                            + jniJson.getProtein().get(getNear(jniJson.getPrs(), aFloat3)).getUnit(),
                    jniJson.getPh().get(getNear(jniJson.getPhs(), aFloat4)).getContentValue() + ""
                            + jniJson.getPh().get(getNear(jniJson.getPhs(), aFloat4)).getUnit(),
                    jniJson.getBlood().get(getNear(jniJson.getBls(), aFloat5)).getContentValue() + ""
                            + jniJson.getBlood().get(getNear(jniJson.getBls(), aFloat5)).getUnit(),
                    jniJson.getSpecificGravity().get(getNear(jniJson.getSpecifis(), aFloat6)).getContentValue() + ""
                            + jniJson.getSpecificGravity().get(getNear(jniJson.getSpecifis(), aFloat6)).getUnit(),
                    jniJson.getKetones().get(getNear(jniJson.getKes(), aFloat7)).getContentValue() + ""
                            + jniJson.getKetones().get(getNear(jniJson.getKes(), aFloat7)).getUnit(),
                    jniJson.getBilirubin().get(getNear(jniJson.getBis(), aFloat8)).getContentValue() + ""
                            + jniJson.getBilirubin().get(getNear(jniJson.getBis(), aFloat8)).getUnit(),
                    jniJson.getGlucose().get(getNear(jniJson.getGls(), aFloat9)).getContentValue() + ""
                            + jniJson.getGlucose().get(getNear(jniJson.getGls(), aFloat9)).getUnit(),
                    jniJson.getAscorbicAcid().get(getNear(jniJson.getAss(), aFloat10)).getContentValue() + ""
                            + jniJson.getAscorbicAcid().get(getNear(jniJson.getAss(), aFloat10)).getUnit()
            };
            int is[] = {
                    Integer.parseInt(dataEntity.getLeukocytes_status().equals("") ? "0" : dataEntity.getLeukocytes_status()),
                    Integer.parseInt(dataEntity.getNitrite_status().equals("") ? "0" : dataEntity.getNitrite_status()),
                    Integer.parseInt(dataEntity.getUrobilinogen_status().equals("") ? "0" : dataEntity.getUrobilinogen_status()),
                    Integer.parseInt(dataEntity.getProtein_status().equals("") ? "0" : dataEntity.getProtein_status()),
                    Integer.parseInt(dataEntity.getPh_status().equals("") ? "0" : dataEntity.getPh_status()),
                    Integer.parseInt(dataEntity.getBlood_status().equals("") ? "0" : dataEntity.getBlood_status()),
                    Integer.parseInt(dataEntity.getGravity_status().equals("") ? "0" : dataEntity.getGravity_status()),
                    Integer.parseInt(dataEntity.getKetones_status().equals("") ? "0" : dataEntity.getKetones_status()),
                    Integer.parseInt(dataEntity.getBilirubin_status().equals("") ? "0" : dataEntity.getBilirubin_status()),
                    Integer.parseInt(dataEntity.getGlucose_status().equals("") ? "0" : dataEntity.getGlucose_status()),
                    Integer.parseInt(dataEntity.getAscorbicacid_status().equals("") ? "0" : dataEntity.getAscorbicacid_status()),

            };


            for (int i = 0; i < history_contents.length; i++) {
                history_contents[i].setName(strings[i]);
                history_contents[i].setContent(floats[i]);
                history_contents[i].setAnInt(is[i]);
            }
            for (int i = 0; i < history_contents.length; i++) {
                arrayListContent.add(history_contents[i]);
            }


            String dayText = (String) day.getText();
            String timeText = (String) time.getText();
            String times[] = {
                    dayText, timeText
            };

            Intent intent = new Intent(context, UHHistoryActivity.class);
            intent.putParcelableArrayListExtra("content", arrayListContent);
            intent.putExtra("days", times);
            if (isIntent) {
                context.startActivity(intent);
            }


//                } catch (Exception e) {
//
//                }
        } catch (Exception e) {

        }
    }

    public static int getNear(float[] array, float target) {
        if (array.length == 0) return -1;
        if (array.length == 1) return 0;
        float[] floa = new float[array.length];
        for (int k = 0; k < floa.length; k++) {
            floa[k] = array[k];
        }
        Arrays.sort(array);
        int min = 0;
        int max = array.length - 1;
        int mid = (int) (min + max) / 2;

        while (max - min > 1) {
            if (target == array[mid]) return mid;
            if (target < array[mid]) max = mid;
            if (target > array[mid]) min = mid;
            mid = (int) (min + max) / 2;
        }

        for (int j = 0; j < floa.length; j++) {
            if (floa[j] == array[array[max] - target < target - array[min] ? max : min]) {
                System.out.println(j + "....." + floa[j]);
                System.out.println("i:" + (array[max] - target < target - array[min] ? max : min));
                return j;
            }
        }
        return array[max] - target < target - array[min] ? max : min;
    }

    private void initImg(String itemString, ImageView imageView) {
        try {
            if (itemString.equals("")) {
                itemString = "0";
            }
            int imghistory = Integer.parseInt(itemString);
            if (imghistory == 0) {
                imageView.setImageResource(R.drawable.history_health);
            } else if (imghistory == 1) {
                imageView.setImageResource(R.drawable.history_bad);
            } else if (imghistory == 2) {
                imageView.setImageResource(R.drawable.history_dangerous);
            } else if (imghistory == 3) {
                imageView.setImageResource(R.drawable.history_health1);
            }
        } catch (Exception e) {
        }
    }


    private class ViewHolder {
        TextView history_date, history_time, ask_doctor;
        RelativeLayout rippleView;
        LinearLayout history_type_LinearL;
        ImageView u1oru2, ask_img;
    }

    public static int get_near(float[] array, float target) {
        if (array.length == 0) return -1;
        if (array.length == 1) return 0;
        float[] floa = new float[array.length];
        for (int k = 0; k < floa.length; k++) {
            floa[k] = array[k];
        }
        Arrays.sort(array);
        int min = 0;
        int max = array.length - 1;
        int mid = (min + max) / 2;

        while (max - min > 1) {
            if (target == array[mid]) return mid;
            if (target < array[mid]) max = mid;
            if (target > array[mid]) min = mid;
            mid = (min + max) / 2;
        }

        for (int j = 0; j < floa.length; j++) {
            if (floa[j] == array[array[max] - target < target - array[min] ? max : min]) {
                return j;
            }
        }
        return (array[max] - target) < (target - array[min]) ? max : min;
    }
}
